<?php
namespace App\Traits;

use App\Services\Api\Api;

trait ApiTrait
{

    /**
     * 发送邮件数据
     * @param  [type] $apiCode [description]
     * @param  [type] $data    [description]
     * @return [type]          [description]
     */
    public function pushEmailData($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'mail_address' => $data['mail_address'],
            'mail_subject' => $data['mail_subject'],
            'mail_content' => $data['mail_content'],
        ];

        $postData = $obj->getPhpServicePostData($apiCode, $formData);
        $result = $obj->getPhpServiceResponseData($postData);
        return $result;
    }

    public function pushCodeTrait($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'code' => $data['code'],
            'phone' => $data['phone'],
            'from' => 'tanwin',
            'type' => $data['type'],
        ];
        $postData = $obj->getPhpServicePostData($apiCode, $formData);
        $result = $obj->getPhpServiceResponseData($postData);
        return $result;
    }

    /**
     * 邮件验证参数生成
     * @param  [type] $phone        [description]
     * @param  [type] $emailAddress [description]
     * @param  [type] $time         [description]
     * @return [type]               [description]
     */
    public function createEmailAuthCode($phone, $emailAddress, $time)
    {
        return md5($phone . '-f-f-' . $emailAddress . '-f-f-' . $time);
    }

    /**
     * 根据领域获取技术路径
     * @param  [type] $apiCode [description]
     * @param  [string] $domains [领域字符串，多个用 , 号隔开]
     * @return [type]          [description]
     */
    public function domainGetTechRoute($apiCode, $domains)
    {
        $obj = new Api();
        $formData = [
            'domainIds' => $domains,
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return $result ? $result['list'] : [];
    }
}
